from myelin.Ns import MYELINS
from myelin.server.core.ft import ModelDriver

from nodetypes.ntypes.Document.showBase.MML import Document, Transform


class Document(Document):
    
    newAgentDriver = staticmethod(ModelDriver.FromRepo)
            
    def createContentContainer(self, context, node, locale):
        model = context.model
        bases = model.mTranslation_of or (model,) 
               
        nxa = node.xml_append
        nxc = node.xml_create_element
        
        for m in bases:
            for mr in m.mFor_Index:
                mt = mr.mPrimary_Type
                if mt:
                    curNode = nxa(
                        nxc(
                            u'a:grouping', MYELINS,
                            attributes={
                                u'role'      : u'index-types',
                                u'node-id'   : mr.rdf_value,
                                u'count'     : unicode(len(mt)),
                            }
                        )
                    )
                    
                    cxa = curNode.xml_append
                    cxc = curNode.xml_create_element   
                    
                    for mt in mt:
                        cxa(
                            cxc(
                                u'a:node', MYELINS,
                                attributes = {
                                    u'uri' : mt.rdf_value,
                                }
                            )
                        )
                
            
        return node
        

class Transform(Transform):
    
    newDocument = Document